//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tools\fn_spawnCrew.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_crew","_vehicle","_grp","_dummy","_dummyType","_crewType","_type","_entry","_hasDriver","_unit","_init","_skill","_instance_id"];
DEFAULT_PARAM(_instance_id,0,TMF_SERVER_INSTANCE_ID);
DEFAULT_PARAM(_vehicle,1,objNull);
DEFAULT_PARAM(_grp,2,grpNull);
DEFAULT_PARAM(_skill,3,([] call FFUNC(getUnitSkill)));
DEFAULT_PARAM(_init,4,[]);
DEFAULT_PARAM(_dummy,5,false);
DEFAULT_PARAM(_dummyType,6,"");
DEFAULT_PARAM(_crewType,7,"");

if (_instance_id == "") then { _instance_id = TMF_SERVER_INSTANCE_ID; };

_crew = [];
if (!IS_BOOL(_dummy)) then { _dummy = false };
if (!IS_STRING(_dummyType)) then { _dummyType = "" };
if (!IS_SCALAR(_skill)) then { _skill = ([] call FFUNC(getUnitSkill)) };
if (!IS_SCALAR(_azi)) then { _azi = 0 };

if ((AND_ALSO(IS_OBJECT(_vehicle),(!isnull _vehicle))) and (AND_ALSO(IS_GROUP(_grp),(!isnull _grp)))) then {
	if (_dummy && _dummyType == "") then { _dummyType = typeof _vehicle};
	
	if (IS_STRING(_crewType)) then {
		_crewType = [_crewType];
	};
	if (!IS_ARRAY(_crewType)) then {
		_crewType = [];
	};
	_type = typeOf _vehicle;
	if (_dummy) then {
		_type = _dummyType;
	};
	_entry = configFile >> "CfgVehicles" >> _type;
	_hasDriver = getNumber (_entry >> "HasDriver");

	if (!_dummy && (OR_ELSE((count _crewType <= 0),OR_ELSE(((_crewType select 0) == ""),!((_crewType select 0) isKindOf "Man"))))) then {
		_crewType = [side _grp, _entry, _type] call FFUNC(getCrewType);
	};
	TRACE_1("Driver Check",ARR_3((_crewType select 0),_hasDriver,(driver _vehicle)));
	if ((_hasDriver >= 1) && (isNull (driver _vehicle))) then {
		if (!_dummy) then {
			_unit = [_instance_id, (_crewType select 0), _grp, (position _vehicle)] call FFUNC(createUnit);
			_crew set [count _crew, _unit];
			if (GVAR(use_external_aiskill==0)) then {
				_unit setUnitAbility _skill;
			};
			_unit moveInDriver _vehicle;
		} else {
			_crew set [count _crew, 1];
		};
	};

	_turrets = [_type] call FFUNC(getVehicleTurrets);

	_funcSpawnTurrets = {
		TRACE_1("Start SpawnTurrets",_this);
		private ["_turrets", "_path"];
		_turrets = _this select 0;
		_path = _this select 1;
		private ["_ix"];
		_ix = 0;
		while {_ix < (count _turrets)} do {
			private ["_turretIndex", "_thisTurret"];
			_turretIndex = _turrets select _ix;
			_thisTurret = _path + [_turretIndex];

			if (!_dummy) then {
				if (isNull (_vehicle turretUnit _thisTurret)) then {
					_unit = [_instance_id,(_crewType select 0), _grp, (position _vehicle)] call FFUNC(createUnit);
					_crew set [count _crew, _unit];
					if (GVAR(use_external_aiskill==0)) then {
						_unit setUnitAbility ([] call FFUNC(getUnitSkill));
					};
					_unit moveInTurret [_vehicle, _thisTurret];
				};
				TRACE_1("Turret Crew",_unit);
			} else {
				_crew set [count _crew, 1];
			};
			[_turrets select (_ix + 1), _thisTurret] call _funcSpawnTurrets;
			_ix = _ix + 2;
		};
		TRACE("End SpawnTurrets");
	};

	[_turrets, []] call _funcSpawnTurrets;

	if (AND_ALSO((count _crewType > 1),(count (_crewType select 1) > 0))) then {
		{
			if (IS_STRING(_x)) then {
				_unit = [_instance_id,_x, _grp, (position _vehicle)] call FFUNC(createUnit);
				if (!isnull _unit) then {
					_crew set [count _crew, _unit];
					if (GVAR(use_external_aiskill==0)) then {
						_unit setUnitAbility _skill;
					};
					_unit moveInCargo _vehicle;
				};
			};
		} forEach (_crewType select 1);
	};

	if (!_dummy) then {
		if (!(isNull (driver _vehicle))) then {(driver _vehicle) setRank "LIEUTENANT"};
		if (!(isNull (gunner _vehicle))) then {(gunner _vehicle) setRank "SERGEANT"};
		if (!(isNull (effectiveCommander _vehicle))) then {(effectiveCommander _vehicle) setRank "CORPORAL"};
	};
} else {
	ERROR("Invalid Parameters");
};

TRACE_1("End",_crew);
TMFPROFILERSTOP;
_crew